<?php

/**
 * Description of class CaptchaHelper used to implement captcha's logic.
 * @author breno
 */ 
class CaptchaHelper extends CApplicationComponent {

    private $_sessionNameErrorLogin = 'errorLogin';
    private $_maxLoginTryWithoutCaptcha = 2;
    private  $_scenarioNameCaptchaRequired = 'captchaRequired';

    /**
     * Increase a session given its session name if it's set
     * if it's unset, set it to 1.
     * @param $sessionName
     * @return void
     */
    public function increaseSession($sessionName) {
        if(isset(Yii::app()->session[$sessionName]))
            Yii::app()->session[$sessionName] = Yii::app()->session[$sessionName] + 1;
        else
            Yii::app()->session[$sessionName] = 1;
    }

    /**
     * Increase the session error login.
     * @return void
     */
    public function increaseSessionErrorLogin() {
        $this->increaseSession($this->_sessionNameErrorLogin);
    }

    /**
     * Unset a session given its session name.
     * @param $sessionName
     * @return void
     */
    public function unsetSession($sessionName) {
        unset(Yii::app()->session[$sessionName]);
    }

    /**
     * Unset the session error login.
     * @return void
     */
    public function unsetSessionErrorLogin() {
        $this->unsetSession($this->_sessionNameErrorLogin);
    }

    /**
     * Return a boolean to check if the user already tried to login more than max times.
     * @return bool
     */
    public function isMaxLoginTryExceeded() {
        if(!isset(Yii::app()->session[$this->_sessionNameErrorLogin]))
            return false;

        return Yii::app()->session[$this->_sessionNameErrorLogin] > $this->_maxLoginTryWithoutCaptcha;
    }

    /**
     * Return the scenario name which a captcha is required.
     * @return string
     */
    public function getScenarioNameCaptchaRequired() {
        return $this->_scenarioNameCaptchaRequired;
    }
}
